package com.xgry.bpm.api.business.function.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xgry.bpm.api.business.function.dao.TBFunctionDao;
import com.xgry.bpm.api.business.function.entity.TBFunction;
import com.xgry.bpm.api.business.function.form.TBFunctionForm;
import com.xgry.bpm.api.business.function.service.TBFunctionService;
import com.xgry.bpm.api.business.function.vo.TBFunctionVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * TBFunction服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-07-14
 */
@Service
public class TBFunctionServiceImpl extends ServiceImpl<TBFunctionDao, TBFunction> implements TBFunctionService {

    @Override
    public int save(TBFunctionForm record) {
        TBFunction data = this.convertDO(record);
        return baseMapper.insert(data);
    }

    @Override
    public int updateById(TBFunctionForm record) {
        //修改时间
        TBFunction data = this.convertDO(record);
        return baseMapper.updateById(data);
    }

    @Override
    public int deleteById(String id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public TBFunctionVO selectById(String id) {
        TBFunction data = baseMapper.selectById(id);
        TBFunctionVO vo = new TBFunctionVO();
        BeanUtils.copyProperties(data, vo);
        return vo;
    }

    @Override
    public List<TBFunctionVO> selectAll(TBFunctionForm record) {
        TBFunction data = this.convertDO(record);
        QueryWrapper<TBFunction> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(data);
        List<TBFunction> list = baseMapper.selectList(queryWrapper);
        return this.convert(list);
    }

    @Override
    public IPage<TBFunctionVO> selectPage(TBFunctionForm record) {
        // form -> do 转换
        TBFunction data = this.convertDO(record);
        // 分页数据设置
        Page<TBFunction> page = new Page<>(record.getCurrent(), record.getSize());
        // 查询条件
        QueryWrapper<TBFunction> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(data);
        IPage<TBFunction> list = baseMapper.selectPage(page, queryWrapper);
        IPage<TBFunctionVO> iPage = new Page<>();
        iPage.setRecords(this.convert(list.getRecords()));
        iPage.setCurrent(list.getCurrent());
        iPage.setSize(list.getSize());
        iPage.setTotal(list.getTotal());
        iPage.setPages(list.getPages());
        return iPage;
    }

    @Override
    public IPage<TBFunction> getPage(IPage<TBFunction> page, QueryWrapper<TBFunction> queryWrapper) {
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public TBFunction findByFunctionName(String vFunctionName) {
        List<TBFunction> queryFunctions = baseMapper.selectList(new QueryWrapper<TBFunction>() {{
            this.eq("v_function_name", vFunctionName);
        }});

        if (CollectionUtils.isEmpty(queryFunctions)) {
            return null;
        }

        return queryFunctions.get(0);
    }

    @Override
    public TBFunction findByFunctionCode(String vFunctionCode) {
        List<TBFunction> queryFunctions = baseMapper.selectList(new QueryWrapper<TBFunction>() {{
            this.eq("v_function_code", vFunctionCode);
        }});

        if (CollectionUtils.isEmpty(queryFunctions)) {
            return null;
        }

        return queryFunctions.get(0);
    }

    @Override
    public TBFunction lastButtons(Integer id) {
        return updateModulerSortUpOrDown(id,true);
    }

    @Override
    public TBFunction nextButtons(Integer id) {
        return updateModulerSortUpOrDown(id,false);
    }

    private TBFunction updateModulerSortUpOrDown(Integer funcitonId , Boolean isUp){
        TBFunction tbFunction = baseMapper.selectById(funcitonId);
        List<TBFunction> tbFunctions = baseMapper.selectList(new QueryWrapper<TBFunction>() {{
            if (isUp) {
                this.le("i_function_sort", tbFunction.getIFunctionSort());
                this.orderByDesc("i_function_sort");
            } else {
                this.ge("i_function_sort", tbFunction.getIFunctionSort());
                this.orderByAsc("i_function_sort");
            }
        }});

        if (CollectionUtils.isEmpty(tbFunctions)) {
            return null;
        }
        TBFunction queryFunction = tbFunctions.get(0);
        Integer iFunctionSort = queryFunction.getIFunctionSort();
        queryFunction.setIFunctionSort(tbFunction.getIFunctionSort());
        tbFunction.setIFunctionSort(iFunctionSort);

        baseMapper.updateById(tbFunction);
        baseMapper.updateById(queryFunction);

        return tbFunction;
    }




    /**
     * Form -> Do
     *
     * @param form 对象
     * @return Do对象
     */
    private TBFunction convertDO(TBFunctionForm form) {
        TBFunction data = new TBFunction();
        BeanUtils.copyProperties(form, data);
        return data;
    }

    /**
     * Do -> VO
     *
     * @param list 对象
     * @return VO对象
     */
    private List<TBFunctionVO> convert(List<TBFunction> list) {
        List<TBFunctionVO> tBFunctionList = new ArrayList<>();
        if (CollectionUtils.isEmpty(list)) {
            return tBFunctionList;
        }
        for (TBFunction source : list) {
            TBFunctionVO target = new TBFunctionVO();
            BeanUtils.copyProperties(source, target);
            tBFunctionList.add(target);
        }
        return tBFunctionList;
    }

}
